#pragma once
#include "Graph.h"
#include <cstring>
//每米人工费+成本 10RMB
#define MONEY 10

typedef char VERTEX;
typedef int WEIGHT;

//建立宿舍楼(创建图）
void CreatGraph(Graph<VERTEX, WEIGHT>& G);


//建设电网(最小生成树)
void CreatPowerGrid(Graph<VERTEX, WEIGHT>& G, Graph<VERTEX, WEIGHT>& minTree);

//DFS + BFS 遍历图
void PrintDormitory(Graph<VERTEX, WEIGHT>& G);
void PrintPowerGrid(Graph<VERTEX, WEIGHT>& G);


//工程报价
void ProjectPrice(Graph<VERTEX, WEIGHT>& minTree);

//打印接口
void InterfacePrint();

//测试
void TestGraphMinTree()
{
	const char* str = "abcdefghi";
	Graph<char, int> g(str, strlen(str));
	g.AddEdge('a', 'b', 4);
	g.AddEdge('a', 'h', 8);
	//g.AddEdge('a', 'h', 9);
	g.AddEdge('b', 'c', 8);
	g.AddEdge('b', 'h', 11); 
	g.AddEdge('c', 'i', 2);
	g.AddEdge('c', 'f', 4);
	g.AddEdge('c', 'd', 7);
	g.AddEdge('d', 'f', 14);
	g.AddEdge('d', 'e', 9);
	g.AddEdge('e', 'f', 10);
	g.AddEdge('f', 'g', 2);
	g.AddEdge('g', 'h', 1);
	g.AddEdge('g', 'i', 6);
	g.AddEdge('h', 'i', 7);
	Graph<char, int> kminTree;
	cout << "Kruskal:" << g.Kruskal(kminTree) << endl;
	kminTree.Print();
}

//程序运行
void ProgramRun()
{
	int run = 0;
	Graph<VERTEX, WEIGHT> G;
	Graph<VERTEX, WEIGHT> minTree;
	bool exchange = false;
	bool build = false;
	do
	{
		InterfacePrint();
		cin >> run;
		switch (run)
		{
			case 1:
				CreatGraph(G);
				exchange = true;
				build = true;
				break;
			case 2:
				if (exchange == true)
					CreatPowerGrid(G, minTree);
				exchange = false;
				break;
			case 3:
				ProjectPrice(minTree);
				break;
			case 4:
				PrintDormitory(G);
				break;
			case 5:
				if (build == true)
					PrintPowerGrid(minTree);
				else
					cout << "还未建设电网 :<" << endl;
				break;
			case 0:
				cout << "感谢使用 ^-^" << endl;
				break;
			default:
				cout << "尚未提供当前功能，请重新输入 :<" << endl;
		}
	} while (run);

}

//建立宿舍楼(创建图）
void CreatGraph(Graph<VERTEX, WEIGHT>& G)
{
	string arr;

	cout << "请输入宿舍号:> ";
	cin >> arr;

	G.Creat(arr.c_str(), arr.size());

	char add;
	cout << "是否为宿舍建立道路(Y/N):";
	cin >> add;

	while (add == 'Y')
	{
		VERTEX src, dst;
		WEIGHT w;
		cout << "宿舍A->宿舍B:距离d:> ";
		cin >> src >> dst >> w;
		G.AddEdge(src, dst, w);
		cout << "是否为宿舍建立道路(Y/N):";
		cin >> add;
	}
}

void CreatPowerGrid(Graph<VERTEX, WEIGHT>& G, Graph<VERTEX, WEIGHT>& minTree)
{
	G.Kruskal(minTree);
	cout << "建设成功 ^-^" << endl;
}

void PrintDormitory(Graph<VERTEX, WEIGHT>& G)
{
	VERTEX v;
	cout << "请输入起始点:> ";
	cin >> v;
	G.BFS(v);
	//G.DFS(v);
}

void PrintPowerGrid(Graph<VERTEX, WEIGHT>& G)
{
	VERTEX v;
	G.Print();
}

void ProjectPrice(Graph<VERTEX, WEIGHT>& minTree)
{
	WEIGHT total = minTree.GetWeight();
	cout << "根据当前宿舍楼分布情况，最优惠的工程价格为:> " << total * MONEY << endl;
}

void InterfacePrint()
{
	cout << endl;
	cout << "==============================" << endl;
	cout << "===" << "      1.建立宿舍楼      " << "===" << endl;
	cout << "===" << "      2.建设电网        " << "===" << endl;
	cout << "===" << "      3.工程报价        " << "===" << endl;
	cout << "===" << "      4.展示宿舍楼      " << "===" << endl;
	cout << "===" << "      5.展示电网        " << "===" << endl;
	cout << "===" << "      按任意键退出      " << "===" << endl;
	cout << "==============================" << endl;
	cout << "你需要的操作是：";
}
